package com.k2data.k2app.service;

import com.google.common.collect.Lists;
import com.k2data.k2app.domain.DictDTO;
import com.k2data.k2app.domain.po.DeviceSpec;
import com.k2data.k2app.mapper.DeviceSpecMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author lidong9144@163.com 17-3-21.
 */
@Service
public class DictService {

    private final DeviceSpecMapper deviceSpecMapper;

    @Autowired
    public DictService(DeviceSpecMapper deviceSpecMapper) {
        this.deviceSpecMapper = deviceSpecMapper;
    }

    public List<DictDTO> selectDeviceType(DeviceSpec cond) {
        List<DictDTO> list = Lists.newArrayList();

        for (DeviceSpec deviceSpec : deviceSpecMapper.select(cond)) {
            DictDTO dto = new DictDTO();
            dto.setId(deviceSpec.getId());
            dto.setLabel(deviceSpec.getValue());
            dto.setValue(deviceSpec.getValue());

            list.add(dto);
        }

        return list;
    }

    public List<DictDTO> selectDeviceSpec(DeviceSpec cond) throws InterruptedException {
        List<DictDTO> list = Lists.newArrayList();
        LinkedBlockingQueue<DeviceSpec> queue = new LinkedBlockingQueue<>();

        for (DeviceSpec deviceSpec : deviceSpecMapper.select(cond)) {
            queue.put(deviceSpec);
        }

        while (!queue.isEmpty()) {
            DeviceSpec innerCond = new DeviceSpec();
            DeviceSpec po = queue.take();
            innerCond.setParentId(po.getId());
            List<DeviceSpec> innerList = deviceSpecMapper.select(innerCond);
            queue.addAll(innerList);
            if (innerList.isEmpty()) {
                DictDTO dto = new DictDTO();
                dto.setId(po.getId());
                dto.setLabel(po.getValue());
                dto.setValue(po.getValue());
                list.add(dto);
            }
        }

        return list;
    }

}
